package com.jason.accountmd.service.localacc;

import com.alibaba.druid.filter.AutoLoad;
import com.jason.accountmd.dao.AccountDao;
import com.jason.accountmd.mapper.AccountMapper;
import com.jason.accountmd.service.AccountService;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import org.apache.shardingsphere.api.hint.HintManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;

@Service
public class LocAccountService implements  ILocAccountServiceApi{
    private static final Logger logger = LoggerFactory.getLogger(LocAccountService.class);
    @Autowired
    private AccountDao accountDao;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public String addAmount(BusinessActionContext businessActionContext, Long innerAccId, BigDecimal amount) {
        logger.info("=======================Loc add===================");
        try {
            accountDao.addAmount(businessActionContext,innerAccId,amount);
            logger.info("给"+innerAccId+"加"+amount);
            logger.info("=======================Loc add end===================");
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
            throw e;
        }
        return "success";
    }

    @Override
    public boolean commitAccount(BusinessActionContext businessActionContext) {
        logger.info("=======================Loc commit acc===================");
        boolean ret = false;
        try {
            Long accId = (Long) businessActionContext.getActionContext("innerAccId");
            HintManager.clear();
            HintManager hintManager = HintManager.getInstance();
            hintManager.addDatabaseShardingValue("t_trans_atmo", accId);
            int result =accountMapper.updateConfirmTransAtmo(businessActionContext.getXid(),businessActionContext.getBranchId());
            ret= result==1?true:false;
            logger.info("直接提交"+ret);
            logger.info("=======================Loc commit end==================="+ret);
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        HintManager.clear();
        return ret;
    }

    @Override
    public boolean rollbackAccount(BusinessActionContext businessActionContext) {
        logger.info("=======================Loc roll begin===================");

        Long accId = (Long) businessActionContext.getActionContext("innerAccId");
        BigDecimal amount = new BigDecimal( businessActionContext.getActionContext("amount")+"");
        Long addAccId = accId+1;
        boolean result = true;
        try {
            accountDao.reduceAmount(businessActionContext,addAccId,amount);
        } catch (Exception e) {
            result = false;
            logger.error(e.getMessage(),e);
        }
        logger.info("=======================Loc roll end==================="+result);
        return result;
    }
}
